home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / The Hacks! / Transport Independent Speech / SpokenSerialApp / ListeningAE.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-20  |  2.7 KB  |  95 lines  |  [TEXT/CWIE]

  1. #ifndef __LISTENINGAE__
  2. #include "ListeningAE.h"
  3. #endif
  4.  
  5. OSErr        SetSpeechDoneAEHander    (SpeechInfoPtr theSpeechInfo)
  6. {
  7.     OSErr        theErr        = noErr;
  8.  
  9.     if (theSpeechInfo->SpeechDoneUPP != nil) {
  10.         DisposeRoutineDescriptor (theSpeechInfo->SpeechDoneUPP);
  11.     }
  12.     theSpeechInfo->SpeechDoneUPP = NewAEEventHandlerProc (HandleSpeechDoneAppleEvent);
  13.  
  14.     return theErr;
  15. }
  16.  
  17. OSErr        InstallRecogAEHandler    (SpeechInfoPtr theSpeechInfo)
  18. {
  19.     OSErr        theErr        = noErr;
  20.  
  21.     /* Install an AppleEvent handler so recognizer can send us recognition results. */
  22.     theErr = AEInstallEventHandler(kAESpeechSuite, kAESpeechDone, theSpeechInfo->SpeechDoneUPP, (long)theSpeechInfo, false);
  23.  
  24.     return theErr;
  25. }
  26.  
  27. pascal OSErr HandleSpeechDoneAppleEvent (AppleEvent *theAEevt, AppleEvent* reply, SpeechInfoPtr theSpeechInfo)
  28. {
  29. #pragma unused (reply)
  30.  
  31.     SRRecognitionResult    recResult;
  32.     DescType            actualType;
  33.     long                actualSize;
  34.     OSErr                theErr            = noErr,
  35.                         recStatus        = noErr;
  36.  
  37.     /* Get status */
  38.     theErr = AEGetParamPtr (theAEevt, keySRSpeechStatus, typeShortInteger, &actualType, (Ptr)&recStatus, sizeof(recStatus), &actualSize);
  39.  
  40.     /* Get result */
  41.     if (theErr == noErr && recStatus == noErr) {
  42.         theErr = AEGetParamPtr (theAEevt, keySRSpeechResult, typeSRSpeechResult, &actualType, (Ptr)&recResult, sizeof(SRRecognitionResult), &actualSize);
  43.     }
  44.     if (theErr == noErr) {
  45.         theErr = ParseResult (&recResult, theSpeechInfo);
  46.     }
  47.  
  48.     return theErr;
  49. }
  50.  
  51. /*
  52.     Here we figure out what the user said and act on what they said.
  53. */
  54. OSErr        ParseResult            (SRRecognitionResult *recResult, SpeechInfoPtr theSpeechInfo)
  55. {
  56.     SRLanguageModel        theResultLM;
  57.     SRPath                theResultPath;
  58.     SRLanguageObject    theSubElement;
  59.     SRSpeechObject        speechRefCon    = 0;
  60.     long                count            = 0,
  61.                         i;
  62.     Size                theLen;
  63.     OSErr                theErr            = noErr;
  64.  
  65.     theLen = sizeof (theResultLM);
  66.     theErr = SRGetProperty (*recResult, kSRLanguageModelFormat, &theResultLM, &theLen);
  67.     if (theErr == noErr) {
  68.         theLen = sizeof (theResultPath);
  69.         theErr = SRGetProperty (*recResult, kSRPathFormat, &theResultPath, &theLen);
  70.         if (theErr == noErr) {
  71.             theErr = SRCountItems (theResultPath, &count);
  72.         }
  73.         if (theErr == noErr) {
  74.             for (i = 0; i < count; i++) {
  75.                 theLen = sizeof (speechRefCon);
  76.                 theErr = SRGetIndexedItem (theResultPath, &theSubElement, i);
  77.                 if (theErr == noErr) {
  78.                     theErr = SRGetProperty (theSubElement, kSRRefCon, &speechRefCon, &theLen);
  79.                     if (speechRefCon != 0) {
  80.                         /* ParseRefCon is the routine that does the work of doing what the
  81.                            user said to do. */
  82.                         theErr = ParseRefCon (theSpeechInfo, (CommandPtr)speechRefCon);
  83.                         SRReleaseObject (speechRefCon);
  84.                     }
  85.                     SRReleaseObject (theSubElement);
  86.                 }
  87.             }
  88.         }
  89.         SRReleaseObject (theResultPath);
  90.     }
  91.  
  92.     SRReleaseObject (theResultLM);
  93.     return theErr;
  94. }
  95.